package com.example.teatea.controller;

import com.example.teatea.LoginApp;
import com.example.teatea.dao.AddStaff;
import com.example.teatea.dao.CheckRegister;
import com.example.teatea.project.Staff;
import com.example.teatea.tools.MD5;
import com.example.teatea.tools.VerifyUtil;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.ResourceBundle;

public class RegisterController implements Initializable {
    public TextField name;
    public TextField phone;
    public ComboBox sex;
    public PasswordField psw;
    public TextField verify;
    public Button returnBt;
    public Button registerBt;
    public ImageView verifyphoto;
    public static String s = new String();
    public static Random random = new Random();
    private static char[] c = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
            'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '3', '5', '6', '7', '8', '9' };

    public static String getRandom(int size) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(c[Math.abs(random.nextInt()) % c.length]);
        }
        s = sb.toString();
        System.out.println("验证码：" + sb);
        return sb.toString();
    }

    public void toReturn(ActionEvent actionEvent) throws Exception {
        Stage stage = (Stage)returnBt.getScene().getWindow();
        stage.close();
        LoginApp loginApp = new LoginApp();
        loginApp.start(stage);
    }

    public void toRegister(ActionEvent actionEvent) throws Exception {
        if(name.getText().equals("")){
            JOptionPane.showMessageDialog(null,"姓名不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(phone.getText().equals("")){
            JOptionPane.showMessageDialog(null,"手机号不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(psw.getText().equals("")){
            JOptionPane.showMessageDialog(null,"密码不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(verify.getText().equals("")){
            JOptionPane.showMessageDialog(null,"验证码不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(!verify.getText().equals(s)){
            JOptionPane.showMessageDialog(null,"验证码错误！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(new CheckRegister().checkRegister(phone.getText())){
            JOptionPane.showMessageDialog(null,"不可重复注册！","提示",JOptionPane.ERROR_MESSAGE);
        }else{
            Staff staff = new Staff();
            MD5 md5 = new MD5();
            staff.setsName(name.getText());
            staff.setsPhone(phone.getText());
            staff.setsSex(sex.getSelectionModel().getSelectedItem().toString());
            staff.setsType("0");
            staff.setsPsw(md5.generateMD5(psw.getText()));
            staff.setsState(0);
            boolean flag = new AddStaff().addStaff(staff);
            if(flag){
                JOptionPane.showMessageDialog(null, "恭喜你，注册成功!", "提示",JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage)returnBt.getScene().getWindow();
                stage.close();
                LoginApp loginApp = new LoginApp();
                loginApp.start(stage);
            }else {
                JOptionPane.showMessageDialog(null, "注册失败!", "提示",JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void toRefresh(MouseEvent mouseEvent) {
        FileOutputStream out = null;
        try{
            out = new FileOutputStream("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
            VerifyUtil.generateVerify(out,getRandom(4));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Image image = new Image("file:E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
        verifyphoto.setImage(image);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        sex.getItems().add("男");
        sex.getItems().add("女");
        sex.getSelectionModel().select(0);
        FileOutputStream out = null;
        try{
            out = new FileOutputStream("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
            VerifyUtil.generateVerify(out,getRandom(4));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Image image = new Image("file:E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
        verifyphoto.setImage(image);
    }
}
